﻿using System;
using System.Drawing;
using System.Windows.Forms;

namespace ThreadSafeControls {
    public partial class ThreadSafeListViewItem {

        private readonly ListViewItem _listViewItem;
        private readonly ThreadSafeListView _listView;
        private readonly ListViewSubItemCollection _subItems;

        internal ThreadSafeListViewItem(ListViewItem listViewItem, ThreadSafeListView listView) {
            if (listViewItem == null)
                throw new ArgumentNullException("listViewItem");
            else if (listView == null)
                throw new ArgumentNullException("listView");

            _listViewItem = listViewItem;
            _listView = listView;
            _subItems = new ListViewSubItemCollection(_listViewItem, _listView);
        }

        internal ListViewItem ListViewItem {
            get { return _listViewItem; }
        }

        public ListViewSubItemCollection SubItems {
            get { return _subItems; }
        }

        public Color BackColor {
            get { return _listView.ReturnAsync(c => _listViewItem.BackColor); }
            set { _listView.UpdateAsync(c => _listViewItem.BackColor = value); }
        }

        public bool Checked {
            get { return _listView.ReturnAsync(c => _listViewItem.Checked); }
            set { _listView.UpdateAsync(c => _listViewItem.Checked = value); }
        }

        public Font Font {
            get { return _listView.ReturnAsync(c => _listViewItem.Font); }
            set { _listView.UpdateAsync(c => _listViewItem.Font = value); }
        }

        public Color ForeColor {
            get { return _listView.ReturnAsync(c => _listViewItem.ForeColor); }
            set { _listView.UpdateAsync(c => _listViewItem.ForeColor = value); }
        }

        public bool Selected {
            get { return _listView.ReturnAsync(c => _listViewItem.Selected); }
            set { _listView.UpdateAsync(c => _listViewItem.Selected = value); }
        }

        public string Text {
            get { return _listView.ReturnAsync(c => _listViewItem.Text); }
            set { _listView.UpdateAsync(c => _listViewItem.Text = value); }
        }

        public void Remove() {
            _listView.UpdateAsync(c => _listViewItem.Remove());
        }

    }
}
